<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择活动状态" clearable class="!w-240px">
          <el-option label="开启" :value="0" />
          <el-option label="关闭" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="活动编号" align="center" prop="id" width="100" />
      <el-table-column label="活动名称" align="center" prop="name" min-width="200" />
      <el-table-column label="活动时间" align="center" min-width="300">
        <template #default="scope">
          {{ formatDate(scope.row.startTime) }} —— {{ formatDate(scope.row.endTime) }}
        </template>
      </el-table-column>
      <el-table-column label="打卡规则" align="center" width="120">
        <template #default="scope">
          {{ getPunchCardRuleText(scope.row.punchCardRule) }}
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" width="120">
        <template #default="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">
            {{ scope.row.status === 0 ? '开启' : '关闭' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openDetail(scope.row.id)">
            查看
          </el-button>
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button
            v-if="scope.row.status === 0"
            link
            type="danger"
            @click="handleClose(scope.row.id)"
          >
            关闭
          </el-button>
          <el-button
            v-else
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RefundActivityForm ref="formRef" @success="getList" />
  
  <!-- 查看详情弹窗 -->
  <DetailModal ref="detailModalRef" />
</template>

<script setup lang="ts">
import { formatDate, dateFormatter } from '@/utils/formatTime'
import RefundActivityForm from './RefundActivityForm.vue'
import DetailModal from './DetailModal.vue'
import * as RefundActivityApi from '@/api/mall/promotion/refund/refundActivity'
import type { RefundActivityRespVO } from '@/api/mall/promotion/refund/refundActivity'

/** 退款活动 */
defineOptions({ name: 'PromotionRefundRule' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<RefundActivityRespVO[]>([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  status: undefined as number | undefined,
  createTime: undefined
})
const queryFormRef = ref() // 搜索的表单
const formRef = ref() // 表单 Ref
const detailModalRef = ref() // 详情弹窗 Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RefundActivityApi.getRefundActivityPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 查看详情 */
const openDetail = (id: number) => {
  detailModalRef.value.open(id)
}

/** 获取打卡规则文本 */
const getPunchCardRuleText = (rule: number) => {
  switch (rule) {
    case 1:
      return '持续打卡'
    case 2:
      return '非持续打卡'
    case 3:
      return '无需打卡'
    default:
      return '未知'
  }
}

/** 关闭按钮操作 */
const handleClose = async (id: number) => {
  try {
    // 关闭的二次确认
    await message.confirm('确认关闭该退款活动吗？')
    // 发起关闭
    await RefundActivityApi.closeRefundActivity(id)
    message.success('关闭成功')
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await RefundActivityApi.deleteRefundActivity(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.time-range-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
</style>